param($installPath, $toolsPath, $package, $project)

$configItem = $project.ProjectItems.Item("Hz.Container.Host.exe.config")

# set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory")
$copyToOutput.Value = 1

# set 'Build Action' to 'Content'
$buildAction = $configItem.Properties.Item("BuildAction")
$buildAction.Value = 2

$project.Save($project.FullName)


# set start action
Function SetProperty($csproj, $group, $name, $value)
{
	if ($group[$name] -eq $null)
	{
		$element = $csproj.CreateElement($name, $csproj.Project.NamespaceURI)
		$element.set_InnerText($value)
		$group.AppendChild($element)
	}
	else
	{
		$group[$name].set_InnerText($value)
	}
}

$csproj = [xml](Get-Content $project.FullName)
foreach ($group in $csproj.Project.PropertyGroup)
{
	if ($group.OutputPath -ne $null)
	{
		if (!$group.OutputPath.StartsWith("`$(MSBuildProjectDirectory)\"))
		{
			$group.OutputPath = "`$(MSBuildProjectDirectory)\" + $group.OutputPath
		}
		SetProperty $csproj $group "StartAction" "Program"
		SetProperty $csproj $group "StartProgram" "`$(OutputPath)Hz.Container.Host.exe"
	}
}
$csproj.Save($project.FullName)


#$configurationManager = $project.ConfigurationManager
#foreach ($name in $configurationManager.ConfigurationRowNames)
#{
#    write-host "projectConfigurations name: " + $name
#    $projectConfigurations = $configurationManager.ConfigurationRow($name)

#    foreach ($projectConfiguration in $projectConfigurations)
#    {                
#        write-host "projectConfiguration name: " + $projectConfiguration

#        $outputPath = $projectConfiguration.Properties.Item("OutputPath")
#        if ($outputPath -ne $null)
#        {
#            if ($outputPath.Value.StartsWith("`$(MSBuildProjectDirectory)\"))
#            {
#                $newOutputPath = $outputPath.Value
#            }
#            else
#            {
#                $newOutputPath = "`$(MSBuildProjectDirectory)\" + $outputPath.Value
#            }
#            $newStartAction = "1"
#            $newStartProgram = "`$(OutputPath)Hz.Container.Host.exe"
#            #$newWorkingDirectory = "`$(OutputPath)"

#            write-host "OutputPath - " $newOutputPath
#            write-host "StartAction - " $newStartAction
#            write-host "StartProgram - " $newStartProgram
#            #write-host "WorkingDirectory - " $newWorkingDirectory

#            $projectConfiguration.Properties.Item("OutputPath").Value = $newOutputPath
#            $projectConfiguration.Properties.Item("StartAction").Value = $newStartAction
#            $projectConfiguration.Properties.Item("StartProgram").Value = $newStartProgram
#            #$projectConfiguration.Properties.Item("StartWorkingDirectory").Value = $newWorkingDirectory
#        }
#    }
#}
#write-host "FullName - " $project.FullName
#$project.Save($project.FullName)
